# 最小版本指定
cmake_minimum_required(VERSION 3.18)

# 工程名
project(net_tuple)

# 开启层级
set_property(GLOBAL PROPERTY USE_FOLDERS On)

if(WIN32)
	add_definitions("/Zi")#调试
	add_definitions("/Od")#禁用优化
	add_compile_options(/W4)
	add_compile_options(/wd4100)
	add_compile_options(/wd4244)
else(WIN32)
endif(WIN32)

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_CURRENT_SOURCE_DIR}/bin)    
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_CURRENT_SOURCE_DIR}/bin) 
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

# 设置环境
add_compile_options("/std:c++latest")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")   
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall") 

# 收集文件
#file(GLOB_RECURSE src_files ${CMAKE_CURRENT_SOURCE_DIR}/src/*.*) 
function(RETRIVE_FILES rootdir out_files)
    #message("root:${rootdir}")
	set(source_list)
    file(GLOB_RECURSE files RELATIVE ${rootdir}
		"${rootdir}/*.cmake"
        "${rootdir}/*.h"
        "${rootdir}/*.hpp"
        "${rootdir}/*.c"
        "${rootdir}/*.cpp"
        "${rootdir}/*.cc"
 	    "${rootdir}/*.def" 	
    )
	#message("files:${files}")
    foreach(filename ${files})
        list(APPEND source_list "${rootdir}/${filename}")
		set(file_abs_path "${rootdir}/${filename}")
		get_filename_component(source_path "${filename}" PATH)
        string(REPLACE "/" "\\" filter_dir "${source_path}")
        source_group("${filter_dir}" FILES "${file_abs_path}")
		#message("filter_dir:${filter_dir}")
		#message("filename:${filename}")
    endforeach()
    set(${out_files} ${source_list} PARENT_SCOPE)
endfunction()

RETRIVE_FILES(${CMAKE_CURRENT_SOURCE_DIR}/src src_files)

add_library(net_tuple SHARED ${src_files})
target_include_directories(net_tuple PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_directories(net_tuple PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib)

# 测试工程
RETRIVE_FILES(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
add_executable(test ${test_files})
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_include_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_directories(test PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib)
target_link_libraries(test net_tuple.lib)
#add_dependencies(test net_tuple)